
/**
 * \file EnvContext.cpp
 *
 *  Created on: 13 apr 2011
 *      Author: ytc0039
 */


#include "EnvContext.h"
#include <AbsSyn/Ident.h>
#include <map>


using namespace std;


EnvContext::EnvContext() {


}

EnvContext::~EnvContext() {



	//CONTEXT_MAP::iterator iter = m_context.begin();


	m_context.clear();

}

bool EnvContext::identExists(const Ident& ident) const
{
	bool ret = false;


	CONTEXT_MAP::const_iterator iter = m_context.find(ident);
	CONTEXT_MAP::const_iterator end = m_context.end();

	if( iter != end )
	{
		ret = true;
	}

	return ret;

}


bool EnvContext::addIdent(TypeEnum type,const Ident& ident)
{
	std::pair< CONTEXT_MAP::iterator , bool > ret;

	//Potentially a problem since there might be 2 of the same name
	ret = m_context.insert( CONTEXT_MAP::value_type(ident,type) );

	assert(ret.second == true);


	return ret.second;
}



bool EnvContext::findType(const Ident& ident,TypeEnum& type)const
{
	CONTEXT_MAP::const_iterator iter = m_context.find(ident);
	bool ret = false;
	if( iter != m_context.end() )
	{
		//Clone it and return it, this way we don't care
		// if someone miss behaves
		type = iter->second;
		ret = true;
	}


	return ret;
}



bool EnvContext::removeIdent(const Ident & ident)
{
	bool ret = false;

	CONTEXT_MAP::iterator iter = m_context.find(ident);

	if( iter != m_context.end() )
	{

		m_context.erase(ident);
		ret = true;

	}

	return ret;
}


void EnvContext::getAllOfType(TypeEnum type, IContext::TypeList& list) const
{
	CONTEXT_MAP::const_iterator it = m_context.begin();
	for(;it != m_context.end() ; ++it)
	{
		TypeEnum mapItemType = it->second;

		if(mapItemType == type)
		{
			//Push the ident into the list which matches the type
			list.push_back(it->first);
		}

	}

}



IContext *EnvContext::getParent() const
{
	return NULL;

}




